package com.westos.echufang.service.regist;


import com.westos.echufang.constants.RegistConstants;
import com.westos.echufang.dao.AdminDao;
import com.westos.echufang.dao.EmployeeDao;
import com.westos.echufang.pojo.entity.Admin;
import com.westos.echufang.pojo.entity.Employee;
import com.westos.echufang.pojo.vo.Regist;
import com.westos.echufang.utils.EmailUtil;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class RegistServiceImpl implements RegistService {

    private final AdminDao adminDao;
    private final EmployeeDao employeeDao;
    private final EmailUtil emailService;

    private final
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    public RegistServiceImpl(AdminDao adminDao, EmployeeDao employeeDao, EmailUtil emailService, BCryptPasswordEncoder bCryptPasswordEncoder) {
        this.adminDao = adminDao;
        this.employeeDao = employeeDao;
        this.emailService = emailService;
        this.bCryptPasswordEncoder = bCryptPasswordEncoder;
    }


    @Override
    public Integer registEmp(Regist regist, String certificate) {
        if (regist != null) {
            String name = regist.getName().trim();
            String phone = regist.getPhone().trim();
            String password = bCryptPasswordEncoder.encode(regist.getPassword());
            String email = regist.getEmail().trim();
            String job = regist.getJob();
            switch (job) {
                case "doctor":
                case "pharmacist":
                    if (StringUtils.isNotBlank(certificate)) {
                        Employee employee = new Employee();
                        employee.setName(name);
                        employee.setPassword(password);
                        employee.setPhone(phone);
                        employee.setEmail(email);
                        employee.setCertificate(certificate);
                        employee.setState("在职");
                        employee.setJob(job);
                        employee.setUid(UUID.randomUUID().toString().replace("-", "").toLowerCase());
                        if (employeeDao.addEmployee(employee) == 1) {
                            return RegistConstants.REGIST_SUCCESS_EMPLOYEE;
                        } else {
                            return RegistConstants.REGIST_FAILED;
                        }
                    } else return RegistConstants.REGIST_FAILED;


                case "admin":
                    Admin admin = new Admin();
                    admin.setName(name);
                    admin.setPassword(password);
                    admin.setPhone(phone);
                    admin.setEmail(email);
                    admin.setUid(UUID.randomUUID().toString().replace("-", "").toLowerCase());
                    System.out.println(admin);
                    if (adminDao.addAdmin(admin) == 1) {
                        return RegistConstants.REGIST_SUCCESS_ADMIN;
                    } else {
                        return RegistConstants.REGIST_FAILED;
                    }
                default:
                    return RegistConstants.REGIST_FAILED;
            }
        } else {
            return RegistConstants.REGIST_FAILED;
        }
    }

    @Override
    public String verifyEmail(String email) {
        if (StringUtils.isNotBlank(email)) {
            String code = RandomStringUtils.random(8, false, true);
            emailService.send(email,code);
            return code;
        } else return "404";
    }

    @Override
    public Integer canRegist(Regist regist) {
        if (regist != null) {
            String phone = regist.getPhone().trim();
            Employee empByPh = employeeDao.getEmpByPh(phone);
            Admin adminByPh = adminDao.getAdminByPh(phone);
            if (empByPh == null && adminByPh == null) {
                return RegistConstants.CAN_REGIST;
            }else
                return RegistConstants.CANNOT_REGIST;
        }else
            return RegistConstants.CANNOT_REGIST;
    }
}
